package com.sunlands.deskmate.web;

import com.sunlands.deskmate.service.PushPayloadService;
import com.sunlands.deskmate.vo.CommonResultMessage;
import com.sunlands.deskmate.vo.PushDTO;
import com.sunlands.deskmate.vo.response.BusinessResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
 * @author shixiaopeng
 */
@SuppressWarnings("unused")
@Api(tags = "极光push")
@Slf4j
@RestController
public class JpushApiController {

    @Autowired
    private PushPayloadService pushPayloadService;

    @ApiOperation(value = "发起push--服务端内部调用")
    @PostMapping("/push")
    public BusinessResult push(@RequestBody PushDTO pushDTO) throws IOException {
        String result = checkData(pushDTO);
        if (result != null) {
            BusinessResult.createInstance(CommonResultMessage.PARAMS_NOT_INVALIDE, result);
        }
        log.info("pushDTO = {}", pushDTO);

        CompletableFuture.runAsync(() -> {
            pushPayloadService.sendPushWithRegIds(pushDTO);
        });
        return BusinessResult.createSuccessInstance(null);
    }


    private String checkData(PushDTO pushDTO) {
        List<String> ids = pushDTO.getIds();
        if (Objects.isNull(ids) || ids.isEmpty()) {
            return "业务Ids不能为空";
        }
        return null;
    }
}
